package com.springmvc.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hibernate.dao.GeoLocationDao;
import com.hibernate.model.Location;

@Controller
@RequestMapping("/puncture")
public class PunctureRestController {
	@Autowired
	GeoLocationDao geoLocationDao;

	@RequestMapping(value = "/{latitude}/{longitude}", method = RequestMethod.GET)
	@ResponseBody
	public List<Location> getNearestLocation(@PathVariable double latitude,
			@PathVariable double longitude) {
		System.out.println("Latitude : " + latitude);
		System.out.println("Longitude : " + longitude);
		List<Location> list = geoLocationDao.getNearestLocation(latitude,
				longitude);
		return list;
	}

	@RequestMapping("/getAllLocations")
	@ResponseBody
	public List<Location> getAllLocations() {
		System.out.println("Request received");
		List<Location> list = geoLocationDao.getAllLocations();
		return list;
	}

	@RequestMapping(value = "/addGpsData/{latitude}/{longitude}/{address}", method = RequestMethod.POST)
	public void insertLocation(@PathVariable double latitude,
			@PathVariable double longitude, @PathVariable String address) {
		Location location = new Location();
		location.setLatitude(latitude);
		location.setLongitude(longitude);
		location.setAddress(address);
		geoLocationDao.insertGeoLocation(location);
		System.out.print("Location inserted");
	}

	public void setGeoLocationDao(GeoLocationDao geoLocationDao) {
		this.geoLocationDao = geoLocationDao;
	}
	
}
